package com.hdaccp.ch08

import java.sql.{Connection, PreparedStatement}

import com.hdaccp.ch07.Mu1
import com.hdaccp.log.MySQLUtils

import scala.collection.mutable.ListBuffer

object MusicDao {
  def insertMu1(list:ListBuffer[Mu1]): Unit = {

    var connection: Connection = null
    var pstmt: PreparedStatement = null

    try {
      //得到数据库连接
      connection = MySQLUtils.getConnection()
     //设置手动提交
      connection.setAutoCommit(false)

      val sql = "insert into mylog4(gender,times) values (?,?) "
      pstmt = connection.prepareStatement(sql)


      for (ele <- list) {
        pstmt.setString(1,ele.gender)
        pstmt.setLong(2,ele.times)
        pstmt.addBatch()
      }

      pstmt.executeBatch() // 执行批量处理
      connection.commit() //手工提交



    } catch {
      case e: Exception => e.printStackTrace()
    } finally {
      MySQLUtils.release(connection, pstmt)
    }
  }



  def insertMu2(list:ListBuffer[Mu2]): Unit = {

    var connection: Connection = null
    var pstmt: PreparedStatement = null

    try {
      //得到数据库连接
      connection = MySQLUtils.getConnection()
      //设置手动提交
      connection.setAutoCommit(false)

      val sql = "insert into mylog5(song,times) values (?,?) "
      pstmt = connection.prepareStatement(sql)


      for (ele <- list) {
        pstmt.setString(1,ele.song)
        pstmt.setLong(2,ele.times)
        pstmt.addBatch()
      }

      pstmt.executeBatch() // 执行批量处理
      connection.commit() //手工提交



    } catch {
      case e: Exception => e.printStackTrace()
    } finally {
      MySQLUtils.release(connection, pstmt)
    }
  }


  def insertMu3(list:ListBuffer[Mu3]): Unit = {

    var connection: Connection = null
    var pstmt: PreparedStatement = null

    try {
      //得到数据库连接
      connection = MySQLUtils.getConnection()
      //设置手动提交
      connection.setAutoCommit(false)

      val sql = "insert into mylog6(types,zd,times) values (?,?,?) "
      pstmt = connection.prepareStatement(sql)


      for (ele <- list) {
        pstmt.setString(1,ele.types)
        pstmt.setString(2,ele.zd)
        pstmt.setLong(3,ele.times)
        pstmt.addBatch()
      }

      pstmt.executeBatch() // 执行批量处理
      connection.commit() //手工提交



    } catch {
      case e: Exception => e.printStackTrace()
    } finally {
      MySQLUtils.release(connection, pstmt)
    }
  }

}
